package com.goldcn.classify.controller;

import com.goldcn.classify.dto.ClazzObjDto;
import com.goldcn.classify.dto.ObjAndAttrDef;
import com.goldcn.classify.model.ClassifyToAttrDef;
import com.goldcn.classify.service.*;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/25.
 */
@RestController
@RequestMapping(value = "/attribute")
@Api(value = "商品分类&类目&品类的属性规格接口")
public class ClassifyAttrController {

    @Autowired
    private ClassifyAttrDefService classifyAttrDefService;

    @Autowired
    private ClassifyAttrValService classifyAttrValService;

    @Autowired
    private ClassifyAttrService classifyAttrService;

    @Autowired
    private ClassifyToAttrDefService classifyToAttrDefService;

    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private StyleService styleService;
    @Autowired
    private PlatformService platformService;


    @RequestMapping(value = "/platform/remove", method = RequestMethod.GET)
    @ApiOperation(value = "移除所有")
    public Object removePlatform() {
        return platformService.removePlatform();
    }

    /**
     * 根据平台类型获取属性列表 4.所有
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/deflist/{code}", method = RequestMethod.GET)
    @ApiOperation(value = "根据平台类型获取属性列表 4.所有")
    public Object loadAttrDefList(@PathVariable String code) {
        return classifyAttrDefService.selectAttrDefList(code);
    }

    /**
     * 根据平台类型获取属性详细资料
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/list/{code}")
    @ApiOperation(value = "根据平台类型获取属性详细资料")
    public Object selectAttributiInfoList(@PathVariable String code) {
        return classifyAttrDefService.selectAttributiInfoList(code);
    }


    /**
     * 根据属性ID获取属性信息详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/def/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据属性ID获取属性信息详情")
    public Object loadAttrDef(@PathVariable String id) {
        if (id == null) {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyAttrDefService.selectByPrimaryKey_new(id);
    }

    /**
     * 根据品类ID或款式ID集合查询所有的属性信息
     *
     * @param list
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "根据品类ID或款式ID集合查询所有的属性信息")
    public Object loadAttrInfo(@RequestBody ClazzObjDto list,
                               @RequestParam(value = "shopID", required = false) String shopID) {
        if (list == null) {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyAttrDefService.loadAttrInfo(list, shopID);
    }

    /**
     * 添加或修改属性信息
     *
     * @param list
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "添加或修改属性信息")
    public Object addAttributes(@RequestBody ClazzObjDto list) {
        if (list == null) {
            throw new ServiceException("参数为空", RestConst.ErrorCode.DATABASE_ERROR);
        }
        if (classifyAttrService.addAttributes(list)) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 添加属性和（品类或款式）关联
     *
     * @param list
     * @return
     */
    @RequestMapping(value = "/AttrAndClazz", method = RequestMethod.POST)
    @ApiOperation(value = "添加属性和（品类或款式）关联")
    public Object relationAttrAndClazz(@RequestBody ObjAndAttrDef list) {
        for (ClassifyToAttrDef classifyToAttrDef : list.getClassifyToAttrDefs()) {
            //判断品类id和属性id是否存在
            if (classifyService.checkByPrimaryKey(classifyToAttrDef.getClazzObjID()) == null &&
                    styleService.checkStyle(classifyToAttrDef.getClazzObjID()) == null) {
                throw new ServiceException("（品类或款式）ID不存在", RestConst.ErrorCode.DATABASE_ERROR);
            }
            classifyAttrDefService.selectByPrimaryKey_new(classifyToAttrDef.getCfadID());
        }
        if (classifyToAttrDefService.addByClazzAndAttr(list)) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 根据属性id查询所有品类或者所有款式信息 objType 0 品类 1 款式 2 全部
     *
     * @return
     */
    @RequestMapping(value = "/objInfo", method = RequestMethod.POST)
    @ApiOperation(value = "根据属性id查询所有品类或者所有款式信息 objType 0 品类 1 款式 2 全部")
    public Object selectClazzObjList(@RequestBody Map<String, String> list) {
        return classifyAttrService.selectClazzObjList(list);
    }

    /**
     * 删除属性和（品类或款式）关联
     *
     * @param list
     * @return
     */
    @RequestMapping(value = "/ClazzToAttr", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除属性和（品类或款式）关联")
    public Object deleteClazzToAttr(@RequestBody ClassifyToAttrDef list) {
        Map<String, String> parm = new HashMap<String, String>();
        parm.put("cfadID", list.getCfadID());
        parm.put("clazzObjID", list.getClazzObjID());
        List<ClassifyToAttrDef> classifyToAttrDefs = classifyToAttrDefService.selectByClazzAndAttr(parm);
        if (classifyToAttrDefs == null || classifyToAttrDefs.size() == 0) {
            throw new ServiceException("删除失败", RestConst.ErrorCode.DATABASE_ERROR);
        }
        String ctaID = classifyToAttrDefs.get(0).getCtaID();
        return classifyToAttrDefService.deleteByPrimaryKey(ctaID);
    }

    /**
     * 删除属性和（品类或款式）关联
     *
     * @param maps
     * @return
     */
    @RequestMapping(value = "/ClazzToAttrlist", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除属性和（品类或款式）关联")
    public Object deleteClazzToAttrList(@RequestBody List<Map<String, String>> maps) {
        return classifyAttrService.deleteObjToAttrList(maps);

    }

    /**
     * 删除属性信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除属性信息")
    public Object deleteAttr(@PathVariable String id) {
        //判断是否与（品类或款式）关联
        Map<String, String> parm = new HashMap<String, String>();
        parm.put("cfadID", id);
        if (!classifyToAttrDefService.selectByClazzAndAttr(parm).isEmpty()) {
            throw new ServiceException("属性与（品类或者款式）存在关联", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyAttrValService.deleteByAttrDef(id);
    }

    /**
     * 根据id集获取属性信息集
     *
     * @param attrIDs
     * @return
     */
    @RequestMapping(value = "/deflist", method = RequestMethod.POST)
    @ApiOperation(value = "根据id集获取属性信息集")
    public Object selectAttrInfo(@RequestBody List<String> attrIDs) {
        List<Map<String, Object>> attrlist = new ArrayList<Map<String, Object>>();
        for (String attrID : attrIDs) {
            attrlist.add(classifyAttrDefService.selectByPrimaryKey_new(attrID));
        }
        return attrlist;
    }
}
